package engine.gui.elements;

import java.util.ArrayList;

import engine.gui.Action;
import engine.gui.Component;
import engine.gui.GUIStyle;

public class RadioButtonGroup extends Component {
	private static final int TEXT_OFFSET = 3;
	private static final int VERTICAL_OFFSET = 3;
	
	private ArrayList<String> entries;
	
	private int selectedIndex = -1;
	
	private Action action;
	
	public RadioButtonGroup(int height) {
		setHeight(height);
		entries = new ArrayList<String>();
	}
	
	public RadioButtonGroup(int posX, int posY, int width, int height) {
		this(height);
		
		setPosX(posX);
		setPosY(posY);
		setWidth(width);
	}
	
	public void addEntry(String entry) {
		entries.add(entry);
	}
	
	public void addEntry(String entry, int position) {
		if(position < entries.size()) {
			entries.add(position, entry);
		} else {
			addEntry(entry);
		}
	}
	
	public void remodeEntry(int position) {
		if(position < entries.size()) {
			entries.remove(position);
		}
	}
	
	@Override
	public void render(int x, int y, int width, int height) {
		GUIStyle style = getCurrentStyle();
		
		int offset = 0;
		for(String entry: entries) {
			renderEntry(style, entry, x, y + offset * (getHeight() + VERTICAL_OFFSET), offset == selectedIndex);
			offset++;
		}
	}
	
	private void renderEntry(GUIStyle style, String text, int x, int y, boolean active) {
		getWindowManager().getGuiShader().sendUniform4f("color", style.getRadioButtonBackgroundColor());
		
		if(style.getRadioButtonBackgroundImage() != null) {
			style.getRadioButtonBackgroundImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
		}
		
		if(active) {
			getWindowManager().getGuiShader().sendUniform4f("color", style.getRadioButtonActiveColor());
			
			if(style.getRadioButtonActiveImage() != null) {
				style.getRadioButtonActiveImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		}
		
		if(text != null) {
			float h = style.getFont().getHeight();
			
			getWindowManager().getGuiShader().sendUniform4f("color", style.getRadioButtonTextColor());
			style.getFont().draw(text, x + getPosX() + getWidth() + TEXT_OFFSET, y + getPosY() + (h + getHeight()) / 2);
		}
	}

	@Override
	public void reset() {
		//do nothing
	}
	
	@Override
	public boolean mousePressed(int posX, int posY, int button) {
		if(button == 0) {
			for(int i = 0; i < entries.size(); i++) {
				if(posX > getPosX() && posX < getPosX() + getWidth() && posY > getPosY() + i * (getHeight() + VERTICAL_OFFSET) && posY < getPosY() + i * (getHeight() + VERTICAL_OFFSET) + getHeight()) {
					setSelectedIndex(i);
					
					if(action != null) {
						action.performAction();
					}
					return true;
				}
			}
		}
		
		return false;
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}
	
	public void setSelectedIndex(int selectedIndex) {
		if(selectedIndex >= entries.size()) {
			selectedIndex = -1;
		} else {
			this.selectedIndex = selectedIndex;
		}
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}
}
